/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.service;

import com.stip.server.da.UserMainDA;
import com.stip.server.da.UserTokenDA;
import com.stip.server.da.UsersHubDA;
import com.stip.server.entities.UserHub;
import com.stip.server.entities.UsersMain;
import com.stip.server.tools.MD5Tools;
import java.security.NoSuchAlgorithmException;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
@Path("account")
public class AccountService {

    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @EJB
    UserMainDA userMainDA;
    @EJB
    UserTokenDA userTokenDA;
    @EJB
    UsersHubDA usersHubDA;
    @GET
    @Path("/{uid}")
    @Consumes(MediaType.APPLICATION_JSON)
    //登录 返回此次登录获得的密匙
    public String login(@PathParam("uid") String uid,@QueryParam("password") String password) throws NoSuchAlgorithmException{
    UsersMain usersMain=userMainDA.findUsersMainById(uid);
        if (usersMain!=null) {
            if (usersMain.getPassword() == null ? password == null : usersMain.getPassword().equals(password)) {
              MD5Tools mdt=new MD5Tools();
              String token=   mdt.tokenMD5(uid);
              return    userTokenDA.editTokenByUID(uid, token);
            } else {
                throw new WebApplicationException(401);
            }
        } else {
            throw  new WebApplicationException(404);
        }
    
    }
    @GET
    @Path("/check/{uid}")
    @Produces(MediaType.APPLICATION_JSON)
    //返回hub表中信息
    public  UserHub signUpFirst(@PathParam("uid") String uid ,@QueryParam("name") String name){
        if (usersHubDA.checkUserHub(uid, name)!=null) {
            return  usersHubDA.checkUserHub(uid, name);
        } else {
            throw  new WebApplicationException(404);
        }
    }
    
    
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    //返回注册产生的第一次产生的密匙
    public String signUpSecond(@QueryParam("uid") String uid,@QueryParam("password")String password) throws NoSuchAlgorithmException{
    UsersMain usersMain=userMainDA.findUsersMainById(uid);
        if (usersMain==null) {
            MD5Tools mD5Tools=new MD5Tools();
            String token=mD5Tools.tokenMD5(uid);
            String newUID=userMainDA.createUserMain(uid, password);
            if (newUID==null) {
                throw  new WebApplicationException(400);
            }
            String key=  userTokenDA.addToken(token, newUID);
            if (key!=null) {
                return  key;
            } else {
                throw new WebApplicationException(400);
            }
        } else {
            throw  new WebApplicationException(401);
        }
    }
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/{uid}")
    //修改密码(需复查)
    public  String changePassword(@PathParam("uid") String uid,@QueryParam("password") String npassword,@QueryParam("token") String token) throws NoSuchAlgorithmException{
    String  checkID= userTokenDA.findToken(token);
     if (checkID == null ? uid == null : checkID.equals(uid)) {
           if (userMainDA.editUserMain(uid, npassword)) {
             MD5Tools mD5Tools=new MD5Tools();
            String newToken=  mD5Tools.tokenMD5(npassword);
            return  userTokenDA.editToken(newToken, token);
         } else {
               throw  new WebApplicationException(400);
         }
 
        } else {
            throw  new WebApplicationException(401);
        }
    
    }
            
}
